package org.zlb.netty.client.rpc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import org.zlb.netty.dbo.RpcResponse;

public class RpcResponseCallback {
    
    private RpcResponse response;
    private boolean isSuccess = false;
    private ReentrantLock lock = new ReentrantLock();
    private Condition callback = lock.newCondition();
    
    public RpcResponse getResponse() {
        lock.lock();
        try {
            if (!isSuccess) {
                callback.await();
            }
        } catch (InterruptedException e) {
             e.printStackTrace();
        } finally {
            lock.unlock();
        }
        
        return response;
    }

    public void setResponse(RpcResponse response) {
        this.response = response;
    }
    
    public boolean isSuccess() {
        return isSuccess;
    }

    public void setSuccess(boolean isSuccess) {
        this.isSuccess = isSuccess;
        lock.lock();
        if (isSuccess) {
            callback.signal();
        }
        lock.unlock();
    }

}
